package com.systemmanger.controller;

import com.bean.WmsTGroup;
import com.bean.WmsTPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.systemmanger.service.PermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR(闫晨航)
 * @date2021-12-08 14:02
 */
@Controller
public class PermController {
    @Autowired
    PermService permService;
    //分组管理页面
    @RequestMapping("xitong_perm.do")
    public String getAllUsers(HttpServletRequest request){
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        List<WmsTPermission> wmsTPermissions = permService.getAllPerms();
        PageInfo<WmsTPermission> pageInfo = new PageInfo<>(wmsTPermissions);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list",list);
        System.out.println(pageInfo);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_perm";
    }
    //分组页面分页
    @RequestMapping("xitong_selectPerm.do")
    public String selectUser(int pageRows, String pageNum,String permID,String permName,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        request.getSession().removeAttribute("permID");
        request.getSession().removeAttribute("permName");

        int pageNum1 = 1;
        if(permName!=null){
            if(!permName.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                System.out.println(permName);
                map.put("permName",permName);
                request.getSession().setAttribute("permName",permName);
            }
        }
        if(permID!=null){
            if(!permID.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                System.out.println(permID);
                map.put("permID",permID);
                request.getSession().setAttribute("permID",permID);
            }
        }
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);
        List<WmsTPermission> wmsTPermissions = permService.getPerms(map);
        PageInfo<WmsTPermission> pageInfo = new PageInfo<>(wmsTPermissions);

        if(pageInfo!=null){
//            System.out.println(pageInfo);
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_perm";
    }
    @RequestMapping("deletePerm.do")
    @ResponseBody
    public void deleteByPermId(String permId, HttpServletResponse response) throws IOException {
        System.out.println(permId);
        String groupId = permService.getGroupid(permId);
        System.out.println(groupId);
        permService.updateIsDel(permId,groupId);
        response.sendRedirect("xitong_perm.do");
    }
}
